package com.lanou.sfu.songforu.download;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class FileUtils {

    private String SDPATH;
    private long mReaded;
    private long mAllSize;
    private DownloadCallBack mDownloadCallBack;
    private static final String TAG = "DOWNLOADAPK";

    public String getSDPATH() {
        return SDPATH;
    }

    public FileUtils() {
        // 得到当前外部存储设备的目录
        // /SDCARD
        SDPATH = Environment.getExternalStorageDirectory() + "/";
    }

    /**
     * 在SD卡上创建文件
     *
     * @throws IOException
     */
    public File creatSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + fileName);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        return file;
    }

    /**
     * 在SD卡上创建目录
     *
     * @param dirName
     */
    public File creatSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
        if (!dir.exists()) {
            dir.mkdir();
        }

        return dir;
    }

    /**
     * 判断SD卡上的文件夹是否存在
     */
    public boolean isFileExist(String fileName) {
        File file = new File(SDPATH + fileName);
        return file.exists();
    }

    /**
     * 将一个InputStream里面的数据写入到SD卡中
     */
    public File writeSD(String path, String fileName, InputStream input) {
        File file = null;
        OutputStream output = null;

        try {
            creatSDDir(path);
            file = creatSDFile(path + "/" + fileName);
            output = new FileOutputStream(file);

            if (mDownloadCallBack != null) {
                mDownloadCallBack.onStart();
            }

            byte buffer[] = new byte[1024 * 10];
            int size = 0;
            long readed = 0;
            while ((size = input.read(buffer)) != -1) {
                output.write(buffer, 0, size);
                readed = readed + size;
                mReaded = readed / 1024;
            }
            output.flush();
        } catch (Exception e) {
            Log.i(TAG, "writeSD_Exception:" + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public File getData(String url, String dir, String fileName)
            throws Exception {
        /* 取得URL */
        URL myURL = new URL(url);
        /* 创建连接 */
        URLConnection conn = myURL.openConnection();
        conn.connect();
        mAllSize = conn.getContentLength() / 1024;
        mReaded = 0;
        Log.i(TAG, "ContentLength:" + mAllSize + "KB");
        /* InputStream 下载文件 */
        InputStream is = conn.getInputStream();
        if (is == null) {
            Log.i(TAG, "stream is null");
            throw new RuntimeException("stream is null");
        }

        return writeSD(dir, fileName, is);
    }

    public synchronized long getReaded() {
        return mReaded;
    }

    public synchronized long getAllSize() {
        return mAllSize;
    }

    public interface DownloadCallBack {
        public void onStart();
        public void onCompleted();
    }

    public void setDownloadCallBack(DownloadCallBack ck) {
        mDownloadCallBack = ck;
    }
}
